package br.com.erpcortume.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.com.erpcortume.beans.Cargo;
import br.com.erpcortume.services.CargoService;

@Controller
@RequestMapping(value = "/cargo")
public class CargoController {

	private static final String CONTEXT_PAGE = "pages";
	
	@Autowired
	private CargoService cargoService;
	
	@RequestMapping(value = "/novo")
	public String novo() {
		return CONTEXT_PAGE+"/cargo/cargo";
	}
	
	@RequestMapping(value = "/salvar", method = RequestMethod.POST)
	public String salvar(Cargo cargo){
		cargoService.salvar(cargo);
		return "redirect:/cargo/listar";
	}
	
	
	@RequestMapping(value = "/listar")
	public ModelAndView listar(){
		
		ModelAndView mav = new ModelAndView(CONTEXT_PAGE+"/cargo/lista");
		List<Cargo> cargos = cargoService.listar();
		
		mav.addObject(cargos);
		
		return mav;
	}
	
	
	@RequestMapping(value = "/toEdit/{id}", method = RequestMethod.GET)
	public ModelAndView toEdit(@PathVariable int id){
		
		ModelAndView mav = new ModelAndView(CONTEXT_PAGE+"/cargo/cargo");
		Cargo cargo = cargoService.find(id);
		
		mav.addObject("cargo", cargo);
		
		return mav;
	}
	
	
/*	@RequestMapping(value = "/editar/{id}", method = RequestMethod.GET)
	public ModelAndView editar(Cargo cargo){
		
		ModelAndView mav = new ModelAndView("cargo/cargo");
		Cargo cargo = cargoService.editar(id);
		
		mav.addObject(cargo);
		
		return mav;
	}*/
	
	@RequestMapping(value = "/excluir/{id}", method = RequestMethod.GET)
	public String excluir(@PathVariable int id){
		cargoService.excluir(id);
		return "redirect:/cargo/listar";
	}
	
	

}
